﻿using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using STeaL.Collections.Specialized;

namespace STeaL.Collections.ObjectModel {

  public abstract class ObservableKeyedCollection<TKey, TItem> : KeyedCollection<TKey, TItem>, INotifyCollectionChanged {
    public ObservableKeyedCollection() : base() { }
    public ObservableKeyedCollection(IEqualityComparer<TKey> comparer) : base(comparer) { }
    public ObservableKeyedCollection(IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold) : base(comparer, dictionaryCreationThreshold) { }

    protected override void InsertItem(int index, TItem item) {
      base.InsertItem(index, item);
      CollectionChanged.NotifyAdd(this, item, index);
    }

    protected override void SetItem(int index, TItem item) {
      base.SetItem(index, item);
      CollectionChanged.NotifyReplace(this, item, index);
    }

    protected override void RemoveItem(int index) {
      TItem item = this[index];
      base.RemoveItem(index);
      CollectionChanged.NotifyRemove(this, item);
    }

    protected override void ClearItems() {
      base.ClearItems();
      CollectionChanged.NotifyReset(this);
    }

    public event NotifyCollectionChangedEventHandler CollectionChanged;
  }
}
